<?php
/**
 * XGPHP 轻量级PHP框架
 * @link http://xgphp.xg3.cn
 * @version 2.0.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @author 讯高科技 <xungaokeji@qq.com>
*/
namespace xg;
use \helper\Arr;
use \xg\facade\Cookie;
use \xg\facade\Config;

class Session {
	protected $config=[];
	function __construct($config=[]) {
		if(!$config) {
			$config=Config::get('session');
		}
		if(is_array($config)) {
			$this->config = $config;
		}
		$this->start();
		$var_session_id=$this->config['var_session_id'];
		if($var_session_id and ($session_id=$_GET[$var_session_id] or $session_id=$_POST[$var_session_id])){
			session_id($session_id);
		}elseif(isset($name['id'])){
			session_id($name['id']);
		}
	}
	public function prefix($prefix=null){
		$this->config('prefix',$prefix);
		return $this;
	}
	public function config($config,$value=null){
		if(is_array($config)){
			$this->config=Arr::extend($this->config,$config);
		}elseif(is_string($config)){
			$this->config[$config]=$value;
		}
		return $this;
	}
	public function regenerate(){
		session_regenerate_id();
		return $this;
	}
	public function pause(){
		session_write_close();
		return $this;
	}
	public function destroy(){
		$_SESSION=array();
		session_unset();
		session_destroy();
		return $this;
	}
	public function start(){
		$conf=$this->config;
		if(isset($conf['name']))session_name($conf['name']);
		if(isset($conf['path']))session_save_path($conf['path']);
		if(isset($conf['domain']))ini_set('session.cookie_domain',$conf['domain']);
		if(isset($conf['expire']))ini_set('session.gc_maxlifetime',$conf['expire']);
		if(isset($conf['use_trans_sid']))ini_set('session.session_use_trans_sid',$conf['use_trans_sid']?1:0);
		if(isset($conf['use_cookies']))ini_set('session.use_cookies',$conf['use_cookies']?1:0);
		if(isset($conf['cache_limiter']))session_cache_limiter($conf['cache_limiter']);
		if(isset($conf['cache_expire']))session_cache_expire($conf['cache_expire']);
		$sess_name=session_name();
		if(session_start()){
			Cookie::set($sess_name,$this->session_id(),'');
		}
		return $this;
	}
	public function clear(){
		$this->remove('user_auth');
		$this->remove('user_auth_sign',null);
		return $this;
	}
	public function remove($name){
		$prefix=$this->config['prefix'];
		if($prefix){
			unset($_SESSION[$prefix][$name]);
		}else{
			unset($_SESSION[$name]);
		}
		return $this;
	}
	public function get($name,$default=null){
		$prefix=$this->config['prefix'];
		$arr=Arr::split($name,'.');
		if($prefix){
			$session_data=$_SESSION[$prefix];
		}else{
			$session_data=$_SESSION;
		}
		while($keyi=array_shift($arr)){
			$session_data=$session_data[$keyi];
		}
		return $session_data?$session_data:$default;
	}
	public function session_id() {
		return session_id();
	}
	public function set($name,$value='') {
		$prefix=$this->config['prefix'];
		if($prefix){
			if(!is_array($_SESSION[$prefix])){
				$_SESSION[$prefix]=array();
			}
			$_SESSION[$prefix][$name]=$value;
		}else{
			$_SESSION[$name]=$value;
		}
		return $this;
	}
}
?>